<?php

declare(strict_types=1);

namespace App\WorkRoom\Controller\Dashboard\Group;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Common\Middleware\DashboardAuthMiddleware;
use App\WorkRoom\Service\WorkRoomGroupService;
use Base\Action\AbstractAction;
use Base\Request\ValidateSceneTrait;

/**
 * 客户群分组管理-列表.
 *
 * Class Index.
 * @Controller
 */
class Index extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var WorkRoomGroupService
     */
    protected $workRoomGroupService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/workRoomGroup/index", methods="get")
     * @return array 返回数组
     */
    public function handle(): array
    {
        ## 接收参数
        $current = $this->request->input('current', 1);
        $size = $this->request->input('size', '10');
        ## 当前登陆者
        $corpId = $this->request->input('userInfo.corp_id');

        ## 组织查询条件
        $where = [];
        $where['corp_id'] = ['corp_id', '=', $corpId];
        $options = [
            'page' => $current,
            'perPage' => $size,
        ];
        ## 查询字段
        $columns = [
            'id',
            'name',
            'corp_id',
            'create_time',
        ];

        $res = $this->workRoomGroupService->getWorkRoomGroupList($where, $columns, $options);

        ## 组织响应数据
        $data = [
            'current'=> $current,
            'size' => $size,
            'total' => 0,
            'pages' => 0,
            'records' => [],
        ];

        if (empty($res['data'])) {
            return $data;
        }
        ## 处理分页数据
        $data['total'] = $res['total'];
        $data['total'] = $res['last_page'];

        ## 处理数据
        foreach ($res['data'] as &$v) {
            $v['workRoomGroupId'] = $v['id'];
            $v['workRoomGroupName'] = $v['name'];
            unset($v['id'], $v['name']);
        }
        $data['records'] = $res['data'];

        return $data;
    }

    /**
     * 验证规则.
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [];
    }
}
